package com.typy.www.tpay.base;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.typy.www.tpay.MainActivity;
import com.typy.www.tpay.R;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.adapter.FriendsAdapter;
import com.typy.www.tpay.ui.view.SwipeRefreshView;
import com.typy.www.tpay.util.LocalDisplay;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.ToastUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.volley.HTTPUtils;
import com.zhy.android.percent.support.PercentLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.OkHttpClient;

/**
 * Created by 13799 on 2018/6/16.
 */

public abstract class PtrBaseContent {
    //全局布局
    public View mView;
    //绑定控件布局
    public ViewHolder holder;
    //环境
    private Context mContext;
    //网络状态
    private boolean mRefreshNeedNetwork = true;
    //是否显示空数据
    private boolean mShowEmptyHint = true;
    //最大数据未设置是默认100条
    private int maxCount = 100;
    //限制数据条数默认10条
    private int mLimt = Constance.LIMT;
    //默认第1页
    private int mPageIndex = 1;
    private int startIndex = 1;
    private boolean hideHint;
    private View foot;

    //PtrBaseContent
    public PtrBaseContent(Context context) {
        mContext = context;
        mView = LayoutInflater.from(context).inflate(getLayoutResId(), null);
        foot = LayoutInflater.from(mContext).inflate(R.layout.activity_empty, null);
        holder = new ViewHolder(mView);
        //初始化SwipeRefreshView
        initEvent();
    }


    public View getmView() {
        return mView;
    }

    //初始化上拉刷新下拉加载功能
    private void initEvent() {
        holder.srRoot.setProgressBackgroundColorSchemeResource(android.R.color.white);
        holder.srRoot.setColorSchemeResources(R.color.colorAccent,
                android.R.color.holo_blue_bright, R.color.colorPrimaryDark,
                android.R.color.holo_orange_dark, android.R.color.holo_red_dark, android.R.color.holo_purple);
        //判断是否设置限制条数
        holder.srRoot.setItemCount(Constance.LIMT);
        //判断是否设置限制条数
        holder.srRoot.measure(0, 0);
        //监听下拉刷新
        holder.srRoot.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadRefreshPage(true);
            }
        });
//        监听上拉加载更多
        holder.srRoot.setOnLoadMoreListener(new SwipeRefreshView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreData();
            }
        });
    }

    //刷新
    public void loadRefreshPage(boolean refresh) {
        if (refresh && mRefreshNeedNetwork && !checkNetwork())
            refresh = false;
        mPageIndex = startIndex;
        getPage(mPageIndex, refresh);
    }

    public void setStartPage(int startPage) {
        startIndex = startPage;
    }

    //加载更多数据
    protected void loadMoreData() {
        int page = startIndex == 1 ? mPageIndex : mPageIndex + 1;
        if (mLimt * page < maxCount) {
            mPageIndex++;
            getPage(mPageIndex, false);
        } else {
            //关闭上拉加载更多
            if (holder.srRoot.isLoading())
                holder.srRoot.setLoading(false);
        }
    }

    //停止刷新动作
    public void loadInitialPage() {
        stopRefresh();
    }

    //停止刷新动作
    public void loadInitialPage(int maxCount) {
        this.maxCount = maxCount;
        showContentOrHint();
        stopRefresh();
    }

    //停止刷新动作
    public void loadInitialPage(int limt, int maxCount) {
        mLimt = limt;
        holder.srRoot.setItemCount(limt);
        this.maxCount = maxCount;
        showContentOrHint();
        stopRefresh();
    }


    private void stopRefresh() {
        if (holder.srRoot.isRefreshing())
            holder.srRoot.setRefreshing(false);
        if (holder.srRoot.isLoading())
            holder.srRoot.setLoading(false);
    }

    public int getLimt() {
        return -1;
    }

    //获取listview
    public ListView getListView() {
        return holder.lvContent;
    }

    //获取listview
    public GridView getGridView() {
        return holder.gvContent;
    }

    //判断界面展示
    public void showContentOrHint() {
        if (holder.lvContent != null) {
            if (holder.lvContent.getFooterViewsCount() == 0) {
                View foot = LayoutInflater.from(mContext).inflate(R.layout.foot_list, null);
                holder.lvContent.addFooterView(foot);
            }
            if (mShowEmptyHint && isEmptyListView()) {
                showHint();

            } else {
                holder.lvContent.removeFooterView(foot);
                showList();
            }
        } else if (holder.gvContent != null) {
            if (mShowEmptyHint && isEmptyGridView()) {
                showHint();
            } else {
                showList();
            }
        }
    }

    //隐藏
    public void hideHint() {
        hideHint = true;
    }


    //显示暂无数据
    private void showHint() {
        holder.flEmpty.setVisibility(View.GONE);
        holder.pbLoading.setVisibility(View.GONE);
        holder.tvLoading.setVisibility(View.GONE);
        holder.lvContent.removeFooterView(foot);
        holder.lvContent.addFooterView(foot);
    }

    //展示加载中
    public void showLoading() {
        holder.flEmpty.setVisibility(View.VISIBLE);
        holder.pbLoading.setVisibility(View.VISIBLE);
        holder.tvLoading.setVisibility(View.VISIBLE);
        holder.tvHint.setVisibility(View.GONE);
    }

    //显示列表
    public void showList() {
        holder.flEmpty.setVisibility(View.GONE);
    }

    //判断是否空数据
    private boolean isEmptyListView() {
        return holder.lvContent.getAdapter() == null || holder.lvContent.getAdapter().isEmpty();
    } //判断是否空数据

    private boolean isEmptyGridView() {
        return holder.gvContent.getAdapter() == null || holder.gvContent.getAdapter().isEmpty();
    }

    //判断网络
    protected boolean checkNetwork() {
        boolean connected = isNetworkConnected();
        if (!connected)
            UiHelper.showToast(mContext, "世界上最遥远的距离是没有网络");
        return connected;
    }

    private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnectedOrConnecting();
    }

    //绑定布局
    static class ViewHolder {
        ProgressBar pbLoading;
        TextView tvLoading;
        TextView tvHint;
        PercentLinearLayout llEmpty;
        LinearLayout llHeadhold;
        ListView lvContent;
        GridView gvContent;
        SwipeRefreshView srRoot;
        LinearLayout llFoothold;
        LinearLayout llHasList;
        RelativeLayout rlRoot;
        FrameLayout flEmpty;
        FrameLayout flHasList;

        ViewHolder(View view) {
            pbLoading = view.findViewById(R.id.pb_loading);
            tvLoading = view.findViewById(R.id.tv_loading);
            tvHint = view.findViewById(R.id.tv_hint);
            llEmpty = view.findViewById(R.id.ll_empty);
            llHeadhold = view.findViewById(R.id.ll_headhold);
            lvContent = view.findViewById(R.id.lv_content);
            srRoot = view.findViewById(R.id.sr_root);
            llFoothold = view.findViewById(R.id.ll_foothold);
            llFoothold = view.findViewById(R.id.ll_hasList);
            rlRoot = view.findViewById(R.id.rl_root);
            flEmpty = view.findViewById(R.id.fl_empty);
            flHasList = view.findViewById(R.id.fl_hasList);
            gvContent = view.findViewById(R.id.gv_content);
        }

    }


    //抽象方法
    //带入界面
    protected abstract int getLayoutResId();

    //获取数据
    protected abstract void getPage(int pageIndex, boolean refresh);


}
